package com.geo.keyword.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 关键词实体类
 * 
 * @author GEO Team
 * @version 1.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("keyword_info")
public class Keyword {
    
    /**
     * 关键词ID
     */
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private String id;
    
    /**
     * 商家ID
     */
    @TableField("merchant_id")
    private String merchantId;
    
    /**
     * 关键词文本
     */
    @NotBlank(message = "关键词不能为空")
    @Size(max = 100, message = "关键词长度不能超过100个字符")
    @TableField("keyword_text")
    private String keywordText;
    
    /**
     * 关键词类型
     */
    @TableField("keyword_type")
    private KeywordType keywordType;
    
    /**
     * 行业类别
     */
    @TableField("industry_category")
    private String industryCategory;
    
    /**
     * 关键词状态
     */
    @TableField("status")
    private KeywordStatus status;
    
    /**
     * 竞争度评分 (0-100)
     */
    @TableField("competition_score")
    private Double competitionScore;
    
    /**
     * 搜索量评分 (0-100)
     */
    @TableField("search_volume_score")
    private Double searchVolumeScore;
    
    /**
     * 优化难度评分 (0-100)
     */
    @TableField("optimization_difficulty")
    private Double optimizationDifficulty;
    
    /**
     * 商业价值评分 (0-100)
     */
    @TableField("commercial_value")
    private Double commercialValue;
    
    /**
     * 当前排名
     */
    @TableField("current_ranking")
    private Integer currentRanking;
    
    /**
     * 目标排名
     */
    @TableField("target_ranking")
    private Integer targetRanking;
    
    /**
     * 优化进度 (0-100)
     */
    @TableField("optimization_progress")
    private Double optimizationProgress;
    
    /**
     * 预期效果
     */
    @TableField("expected_effect")
    private String expectedEffect;
    
    /**
     * 关键词标签 (JSON格式)
     */
    @TableField("tags")
    private String tags;
    
    /**
     * 相关关键词 (JSON格式)
     */
    @TableField("related_keywords")
    private String relatedKeywords;
    
    /**
     * 长尾关键词 (JSON格式)
     */
    @TableField("long_tail_keywords")
    private String longTailKeywords;
    
    /**
     * AI分析结果 (JSON格式)
     */
    @TableField("ai_analysis_result")
    private String aiAnalysisResult;
    
    /**
     * 优化策略 (JSON格式)
     */
    @TableField("optimization_strategy")
    private String optimizationStrategy;
    
    /**
     * 监控频率 (小时)
     */
    @TableField("monitor_frequency")
    private Integer monitorFrequency;
    
    /**
     * 上次监控时间
     */
    @TableField("last_monitored_at")
    private LocalDateTime lastMonitoredAt;
    
    /**
     * 下次监控时间
     */
    @TableField("next_monitor_at")
    private LocalDateTime nextMonitorAt;
    
    /**
     * 是否活跃监控
     */
    @TableField("is_active_monitoring")
    private Boolean isActiveMonitoring;
    
    /**
     * 备注
     */
    @TableField("remark")
    private String remark;
    
    /**
     * 创建时间
     */
    @TableField(value = "created_at", fill = FieldFill.INSERT)
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;
    
    /**
     * 创建者ID
     */
    @TableField("created_by")
    private String createdBy;
    
    /**
     * 更新者ID
     */
    @TableField("updated_by")
    private String updatedBy;
    
    /**
     * 逻辑删除标志
     */
    @TableLogic
    @TableField("deleted")
    private Integer deleted;
    
    /**
     * 关键词类型枚举
     */
    public enum KeywordType {
        BRAND("brand", "品牌关键词"),
        PRODUCT("product", "产品关键词"),
        SERVICE("service", "服务关键词"),
        INDUSTRY("industry", "行业关键词"),
        LONG_TAIL("long_tail", "长尾关键词"),
        COMPETITOR("competitor", "竞品关键词");
        
        private final String code;
        private final String description;
        
        KeywordType(String code, String description) {
            this.code = code;
            this.description = description;
        }
        
        public String getCode() {
            return code;
        }
        
        public String getDescription() {
            return description;
        }
        
        public static KeywordType fromCode(String code) {
            for (KeywordType type : values()) {
                if (type.code.equals(code)) {
                    return type;
                }
            }
            return PRODUCT;
        }
    }
    
    /**
     * 关键词状态枚举
     */
    public enum KeywordStatus {
        ANALYZING("analyzing", "分析中"),
        ACTIVE("active", "活跃"),
        OPTIMIZING("optimizing", "优化中"),
        PAUSED("paused", "暂停"),
        COMPLETED("completed", "已完成"),
        FAILED("failed", "失败");
        
        private final String code;
        private final String description;
        
        KeywordStatus(String code, String description) {
            this.code = code;
            this.description = description;
        }
        
        public String getCode() {
            return code;
        }
        
        public String getDescription() {
            return description;
        }
        
        public static KeywordStatus fromCode(String code) {
            for (KeywordStatus status : values()) {
                if (status.code.equals(code)) {
                    return status;
                }
            }
            return ANALYZING;
        }
    }
    
    /**
     * 检查关键词是否处于活跃状态
     * @return 是否活跃
     */
    public boolean isActive() {
        return status == KeywordStatus.ACTIVE || status == KeywordStatus.OPTIMIZING;
    }
    
    /**
     * 检查是否需要监控
     * @return 是否需要监控
     */
    public boolean needsMonitoring() {
        return Boolean.TRUE.equals(isActiveMonitoring) && 
               isActive() && 
               (nextMonitorAt == null || LocalDateTime.now().isAfter(nextMonitorAt));
    }
    
    /**
     * 获取优化难度等级
     * @return 难度等级
     */
    public String getDifficultyLevel() {
        if (optimizationDifficulty == null) return "未知";
        
        if (optimizationDifficulty <= 30) return "简单";
        else if (optimizationDifficulty <= 60) return "中等";
        else if (optimizationDifficulty <= 80) return "困难";
        else return "极难";
    }
    
    /**
     * 获取竞争度等级
     * @return 竞争度等级
     */
    public String getCompetitionLevel() {
        if (competitionScore == null) return "未知";
        
        if (competitionScore <= 25) return "低";
        else if (competitionScore <= 50) return "中";
        else if (competitionScore <= 75) return "高";
        else return "极高";
    }
    
    /**
     * 计算下次监控时间
     */
    public void calculateNextMonitorTime() {
        if (monitorFrequency != null && monitorFrequency > 0) {
            this.nextMonitorAt = LocalDateTime.now().plusHours(monitorFrequency);
        }
    }
    
    // 手动添加setter方法以解决编译错误
    public void setId(String id) {
        this.id = id;
    }
    
    public void setKeywordText(String keywordText) {
        this.keywordText = keywordText;
    }
    
    public void setIndustryCategory(String industryCategory) {
        this.industryCategory = industryCategory;
    }
    
    public void setRemark(String remark) {
        this.remark = remark;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public void setDeleted(Integer deleted) {
        this.deleted = deleted;
    }
    
    // 添加更多缺失的setter方法
    public void setMerchantId(String merchantId) {
        this.merchantId = merchantId;
    }
    
    public void setKeywordType(KeywordType keywordType) {
        this.keywordType = keywordType;
    }
    
    public void setStatus(KeywordStatus status) {
        this.status = status;
    }
    
    public void setOptimizationDifficulty(Double optimizationDifficulty) {
        this.optimizationDifficulty = optimizationDifficulty;
    }
    
    public void setCommercialValue(Double commercialValue) {
        this.commercialValue = commercialValue;
    }
    
    public void setSearchVolumeScore(Double searchVolumeScore) {
        this.searchVolumeScore = searchVolumeScore;
    }
    
    public void setAiAnalysisResult(String aiAnalysisResult) {
        this.aiAnalysisResult = aiAnalysisResult;
    }
    
    public void setLastMonitoredAt(LocalDateTime lastMonitoredAt) {
        this.lastMonitoredAt = lastMonitoredAt;
    }
    
    // 添加缺失的getter方法
    public String getKeywordText() {
        return keywordText;
    }
}